import cv2

# 1. 初始化视频捕获对象
cap = cv2.VideoCapture("../images/input_video.mp4")

# 2. 判断是否打开成功
if not cap.isOpened():
    print("视频打开失败")
    exit()

# 3. 循环读取视频帧  (帧就是张的意思)
frame_count = 0   # 总帧数
save_count = 0    # 保存的帧数
while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break
    if frame_count % 30 == 0:    # 每隔30帧保存一帧
        cv2.imwrite(f"frame_{save_count}.jpg", frame)
        print(f"保存第{save_count}帧")
        save_count += 1
    frame_count += 1

# 4. 释放资源
cap.release()
print("视频处理完毕")